package com.hqw.jzdemo.util;

import android.util.Log;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class LogUtil {

//    public static boolean isDebug = BuildConfig.DEBUG; // 是否需要打印bug，可以在Application的onCreate函数里面初始化
    public static boolean isDebug = true; // TODO-上线前关闭

    // 可以通过广播打开LOG：adb shell am broadcast -a "${broadcastAction}"
    public static final String broadcastAction = "com.huanqiu.news.util.LogUtil";

    // log2file config
    private static boolean V_log2file = false; // 默认 false
    private static boolean I_log2file = false; // 默认 false
    private static boolean D_log2file = false; // 默认 false
    private static boolean W_log2file = true; // 默认 true
    private static boolean E_log2file = true; // 默认 true

    private LogUtil() {
        /* cannot be instantiated */
        throw new UnsupportedOperationException("cannot be instantiated");
    }

    public static void v(String tag, String msg) {
        if (isDebug) {
            Log.v(tag, msg);
            if (V_log2file)
                log2file("V/" + tag, msg, null);
        }
    }

    public static void i(String tag, String msg) {
        if (isDebug) {
            Log.i(tag, msg);
            if (I_log2file)
                log2file("I/" + tag, msg, null);
        }
    }

    public static void d(String tag, String msg) {
        if (isDebug) {
            Log.d(tag, msg);
            if (D_log2file)
                log2file("D/" + tag, msg, null);
        }
    }

    public static void w(String tag, String msg) {
        if (isDebug) {
            Log.w(tag, msg);
            if (W_log2file)
                log2file("W/" + tag, msg, null);
        }
    }

    public static void e(String tag, String msg) {
        if (isDebug) {
            Log.e(tag, msg);
            if (E_log2file)
                log2file("E/" + tag, msg, null);
        }
    }

    // 打开 LOG 开关
    public static void enableLog() {
        isDebug = true;
        LogUtil.d("aaa", "LOG开关已打开");
    }

    // 获取 Throwable 的堆栈信息
    public static String getThrowableMsg(Throwable t) {
        if (t == null)
            return "";

        StringBuilder sb = new StringBuilder();
        sb.append(t.toString());
        sb.append("\n");

        StackTraceElement[] stack = t.getStackTrace();
        for (StackTraceElement a : stack) {
            sb.append("\t\t\t");
            sb.append(a.toString());
            sb.append("\n");
        }

        return sb.toString();
    }

    // 把log写入文件
    private static synchronized void log2file(String tag, String msg, Throwable t) {
        //String filename = "/sdcard/" + Deeper.getContext().getPackageName() + "/log_20150101.txt";
        String dir = "/sdcard";
        String filename = "log_svoc_20170101.txt";

        File file = new File(dir);
        if (!file.exists()) {
            file.mkdir();
        }

        // 在 tag 前添加当前时间
        tag = getCurTime() + " " + tag;

        RandomAccessFile raf = null;
        try {
            raf = new RandomAccessFile(dir + "/" + filename, "rw");
            raf.seek(raf.length());

            raf.write(tag.getBytes());
            raf.write(": ".getBytes());
            raf.write(msg.getBytes());
            raf.write("\n".getBytes());

            if (t != null) {
                raf.write(getThrowableMsg(t).getBytes());
                raf.write("\n".getBytes());
            }
            raf.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (raf != null) {
                try {
                    raf.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private static String getCurTime() {
        long time = System.currentTimeMillis();
        Date now = new Date(time);
        DateFormat df = new SimpleDateFormat("MM-dd HH:mm:ss.");
        return df.format(now) + String.format(Locale.getDefault(), "%03d", time % 1000) ;
    }

}
